;layui.define(function(e) {
    layui.use(['table','layer','form','laydate'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;

        var currPage = 1;
        //展示已知数据
        table.render({
            elem: '#idTest'
            , id: 'listReload'//生成 Layui table 的标识 id，必须提供，用于后文刷新操作
            , url: hostUrl + '/orderBook/orderBookList'
            ,cols: [[ //标题栏
            	{field: 'order_id', title: '订单号'}
            	,{field: 'customer_name', title: '客户姓名'}
                ,{field: 'brand_name', title: '品牌'}
                ,{field: 'series_name', title: '车系'}
                ,{field: 'car_type_name', title: '车型', minWidth: 120}
                ,{field: 'factory_price', title: '指导价(元)'}
                ,{field: 'purchase_price', title: '采购价(元)'}
                ,{field: 'deal_price', title: '成交价(元)'}
                ,{field: 'car_frame_num', title: '车架号',minWidth: 160}
                ,{field: 'financial_products_name', title: '金融方案'}
                ,{field: 'financial_price', title: '融资额(元)'}
                ,{field: 'onlycarincome', title: '单车综合收入(元)',minWidth: 160}
                ,{field: 'onlycarWholeProfit', title: '单车综合毛利(元)',minWidth: 160}
                ,{field: 'onlycarWholeProfitRatio', title: '单车综合毛利率',minWidth: 160}
                ,{field: 'store_sale_acc_name', title: '销售顾问'}
                ,{field: 'brokerName', title: '经纪人'}
                ,{field: 'tool',title: '操作',fixed: 'right', minWidth:188, align:'center', toolbar: '#barDemo'}
               
            ]]
            ,height: 500
            ,cellMinWidth: 120
            ,page: true //是否显示分页
            , done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                //得到当前页码
                currPage = curr;
                $('.layui-table-fixed-r').removeClass('layui-hide');
            }
        });
        //表刷新方法
        var reloadTable = function () {

            table.reload("listReload", { //此处是上文提到的 初始化标识id
                where: {
                    customerName: $("#customerName").val(), 
                    shopTimes: $("#shopTimes").val(),
                    carPeriod: $("#carPeriod").val(),
                    startDate: $("#startDate").val(),
                    endDate: $("#endDate").val()
                }, page: {
                    curr: currPage //重新从第 page 页开始 默认从第一页开始重载
                }
            });
        };

        // 查询按钮绑定点击事件
        $("#searchBtn").on("click", function () {
            reloadTable();
        })

        //日期控件
        layui.use(['laydate'], function(){
            var laydate = layui.laydate;
            //常规用法
            laydate.render({
                elem: '#startDate'
            });

            laydate.render({
                elem: '#endDate'
            });
        });

        
    //监听工具条
    table.on('tool(listTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'details'){
        	dataDetail=data;
        	var detail=layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '销售报表详情'
                ,content: hostUrl+'/retailorder/retailorderDetailBook.do'
                ,area:  ['500px', '500px']
                ,maxmin: true
                ,end: function(){
                	dataDetail=null;
                }
            }); 
        	layer.full(detail);
         }  
    });

        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('idTest');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };

        $('.layadmin-takerates .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
         
        //执行一个laydate实例
        laydate.render({
            elem: '#date'
        });

        form.render();

        //订单池：按钮“新建订单”
        $("#retailorderAdd").click(function () {
            Layer.openDialogWithCallBack("修改资源", hostUrl+'/retailorder/retailorderAdd.do?oper=add',"500px","800px",function () {
                location.reload();
            });
        });

         //车辆信息：点击按钮“新建客户”
         $("#newCustomers").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '新建客户'
                ,content: 'newCustomers.html'
                ,area: ['500px', '500px']
                ,skin:'my_skin'
                ,maxmin: true
            });
            layer.full(index);
        });

        $(".check_more").click(function () {
            var index = layer.tab({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '更多参数配置'
                ,content: 'more.html'
                ,area: ['500px', '700px']
                ,maxmin: true
//                 ,success: function(layero, index){
//                     弹框加载成功页面参数传递给弹框
//                     var body = layer.getChildFrame('body', index);
//                     var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
// //                        console.log(body.html()) //得到iframe页的body内容
//                     var paramId=body.find("input[name='id']");
//                     paramId.val(111);
//                 }
            });
            layer.full(index);
        })
        
    });

    
    
    //输出模块
    e("list", {})
});